package com.cnhis.cloudhealth.clinical.hosnurse.hospatientouthospital.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hospatientouthospital.dao.HosPatientOutHospitalDao;
import com.cnhis.cloudhealth.clinical.hosnurse.hossendadvice.bo.HosSendAdviceBo;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONArray;
@SuppressWarnings({"rawtypes","unchecked"})
@Service
public class HosPatientOutHospitalBo {
    @Resource
    private HosPatientOutHospitalDao hosPatientOutHospitalDao;
    @Resource
    private GetNewId getNewId;
    @Autowired
    private HosSendAdviceBo hosSendAdviceBo;

	private ModelVo packModelVoPage(String nid,Map map,int pageno,int pagesize) {
		ModelVo mo = new ModelVo();
		try {
			Page p = hosPatientOutHospitalDao.queryForPage(nid, map, pageno, pagesize);
			if (p.getList() != null && p.getList().size() > 0) {
				mo.setList(p.getList());
				mo.setResult(StaticKeys.OH_SUCCESS);
				mo.setTotal(p.getTotalNumber());
				mo.setRows(p.getList());
				mo.setPage(pageno);
				mo.setRecords(p.getTotalNumber());
				mo.setTotalPage(p.getTotalPage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mo;
	}
    public ModelVo outHospitalCase()throws Exception{
    	ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientOutHospitalDao.outHospitalCase();
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
    }
    public ModelVo outHospitalTreatment(Map map,int currPage,int pageSize)throws Exception{
		return packModelVoPage("HosPatientOutHospital.outHospitalTreatment",map,currPage,pageSize);
    }
    public ModelVo outHospitalWay()throws Exception{
    	ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientOutHospitalDao.outHospitalWay();
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
    }
    private Map param(Map map)throws Exception{
    	return hosPatientOutHospitalDao.param(map);
    } 
    private boolean checkPatientState(Map map)throws Exception{
    	return hosPatientOutHospitalDao.checkPatientState(map)==0;
    }
    private boolean checkPatientPathWay(Map map)throws Exception{
    	return hosPatientOutHospitalDao.checkPatientPathWay(map)>0;
    }
    private boolean checkdate(Map map)throws Exception{
    	return hosPatientOutHospitalDao.checkdate(map)==0;
    }
    public ModelVo execNursePatBringForwardHosp(Map map)throws Exception{
    	ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
    	String lXml=  map.get("lXml").toString();
    	String lPatXml= map.get("lPatXml").toString();
    	List<Map<String,Object>> TmpXml = JsonUtil.parserToList(JSONArray.fromObject(lXml));
    	Map<String,Object> PatXml = JsonUtil.parserToMap(lPatXml);
    	map.put("TmpXml", TmpXml);
    	map.put("PatXml", PatXml);
    	map.put("lVAE01", PatXml.get("vae01").toString());	
    	map.put("lVAA01", PatXml.get("vaa01").toString());
    	map.put("lFdate", PatXml.get("vae26").toString());
    	map.put("adate", DateUtils.getDateString(new Date()));
    	map.remove("lXml");
    	map.remove("lPatXml");
    	Map map1 = param(map);
    	map.putAll(map1);
    	if(!checkdate(map)){
    		mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("病人有医嘱停嘱时间大于出院时间,请重新调整出院时间或者修改停嘱时间!");
			return mo;
    	}
    	//病人是否在院
    	if(checkPatientState(map)){
    		mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("病人不是在院状态,不能进行预出院操作");
			return mo;
    	}
    	if(checkPatientPathWay(map)){
    		mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("病人还没退出临床路径,不能进行预出院操作");
			return mo;
    	}
    	if("1".equals(map1.get("param205").toString())){
    		//获取病人信息
    		List tmpVAP = getVAP1(map);
    		map.put("tmpVAP", tmpVAP);
    		//修改病人护理记录
    		hosPatientOutHospitalDao.updateVCF1(map);
    	}
    	//修改病人登记信息、病人信息、病人床位信息
    	hosPatientOutHospitalDao.updateVAE1_VAA1_BCQ1(map);
    	//新增病人出院诊断、
//    	updateVAO2(map);
    	//新增病人变更记录
    	updateVBO1(map);
    	//修改病人转科记录、删除病人自动记账项目
    	hosPatientOutHospitalDao.updateVBP1_BEO1(map);
    	//修改病人余额
    	updateVBM1(map);
    	return mo;
    	
    }
    private List getVAP1(Map map)throws Exception{
    	List<Map<String,Object>> tmpVAP = hosPatientOutHospitalDao.getVAP1(map);
    	for(Map<String,Object> mm:tmpVAP){
    		mm.put("vcf01",getNewId.nextId());
    	}
    	return tmpVAP;
    }
    private List getTmp(Map map)throws Exception{
    	List<Map<String,Object>> tmp = hosPatientOutHospitalDao.getTmp(map);
    	for(Map<String,Object> mm:tmp){
    		mm.put("vaoid",getNewId.nextId());
    	}
    	return tmp;
    }
   private void updateVAO2(Map map)throws Exception{
	   map.put("TmpXml",getTmp(map));
	   hosPatientOutHospitalDao.updateVAO2(map);
   }
   public void updateVBO1(Map map)throws Exception{
	   map.put("VBO01",getNewId.nextId());
	   hosPatientOutHospitalDao.updateVBO1(map);
   }
   public void updateVBM1(Map map)throws Exception{
	   Map hs = new HashMap();
		hs.put("lType", 0);
		hs.put("lBCK01", 0);
		hs.put("lVAA01", map.get("lVAA01").toString());
		hs.put("lMoney", 0);
		hs.put("lVBM03", 2);
		hs.put("VAA07", map.get("lVAE01").toString());
		hosSendAdviceBo.execNurseVBM1Update(hs);
   }
   public ModelVo outHospitalTreatment1(int currPage,int pageSize)throws Exception{
	   ModelVo mo = new ModelVo();
		Page pg =new Page();
		try {
			pg=hosPatientOutHospitalDao.outHospitalTreatment1(currPage, pageSize);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(pg.getList());
		return mo;
   }
	public ModelVo chineseMedicineTreatment(int lCAM02)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosPatientOutHospitalDao.chineseMedicineTreatment(lCAM02);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	
	public ModelVo chineseMedicineTreatment1(int currPage,int pageSize)throws Exception{
		  ModelVo mo = new ModelVo();
			Page pg =new Page();
			try {
				pg=hosPatientOutHospitalDao.chineseMedicineTreatment1(currPage, pageSize);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg.getList());
			return mo;
	}
}
